package ms.jasim.model.editor.views;

import java.util.ArrayList;
import java.util.List;

import ms.jasim.framework.SolutionSimulator;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;

import swing2swt.layout.BorderLayout;

public class SolutionSimulationView extends ViewPart implements ISelectionProvider {

	private static final String NO_SIMULATION_TO_VIEW = "No simulation to view";
	private StyledText styledText;
	public static final String ID = "ms.jasim.model.editor.views.SolutionSimulationView"; //$NON-NLS-1$
	private List<SolutionSimulator> simulators;
	private CCombo combo;
	private ArrayList<ISelectionChangedListener> listeners;

	public SolutionSimulationView() {
		simulators = new ArrayList<SolutionSimulator>();
	}
	
	/**
	 * Create contents of the view part
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		container.setLayout(new BorderLayout(0, 0));

		/*final Composite composite = new Composite(container, SWT.BORDER);
		composite.setLayoutData(BorderLayout.NORTH);
		composite.setLayout(new BorderLayout(0, 0));

		label = new Label(composite, SWT.NONE);
		label.setText(NO_SIMULATION_TO_VIEW);
		label.setLayoutData(BorderLayout.CENTER);*/
		
		combo = new CCombo(container, SWT.BORDER | SWT.READ_ONLY);
		combo.setLayoutData(BorderLayout.NORTH);
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				viewSimulator();
			}
		});

		final CTabFolder tabFolder = new CTabFolder(container, SWT.NONE);
		tabFolder.setTabPosition(SWT.BOTTOM);
		tabFolder.setLayoutData(BorderLayout.CENTER);

		final CTabItem textViewTabItem = new CTabItem(tabFolder, SWT.NONE);
		textViewTabItem.setText("Text View");

		styledText = new StyledText(tabFolder, SWT.V_SCROLL | SWT.READ_ONLY | SWT.H_SCROLL);
		textViewTabItem.setControl(styledText);
		
		tabFolder.setSelection(textViewTabItem);
		
		getSite().setSelectionProvider(this);
		//
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	/**
	 * Create the actions
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
	
	public void setInput(SolutionSimulator sim) {
		simulators.clear();
		if (sim != null) {
			simulators.add(sim);
			setInput(simulators);
		}
		updateInput();
	}
	
	private void updateInput() {
		combo.removeAll();
		if (simulators.size() > 0) {
			for (SolutionSimulator sim: simulators)
				combo.add(sim.getSolution().getName());
			combo.setText(simulators.get(0).getSolution().getName());
			viewSimulator();
		}
		else
			styledText.setText("");
	}

	public void setInput(List<SolutionSimulator> result) {
		this.simulators.clear();
		if (result != null)
			simulators.addAll(result);
		updateInput();
	}

	private void viewSimulator() {
		int idx = combo.getSelectionIndex();
		if (simulators != null && idx >=0 && simulators.size() > idx) {
			styledText.setText(simulators.get(idx).convertTimeSlotsToString());
		}
		else
			styledText.setText("");
		setSelection(getSelection());
	}

	@Override
	public void addSelectionChangedListener(ISelectionChangedListener listener) {
		if (listeners ==  null)
			listeners = new ArrayList<ISelectionChangedListener>();
		listeners.add(listener);
	}

	@Override
	public ISelection getSelection() {
		int idx = combo.getSelectionIndex();
		SolutionSimulator sim = (simulators != null && idx >=0 && simulators.size() > idx) ? simulators.get(idx) : null;
		return sim != null? new StructuredSelection(sim) : null;
	}

	@Override
	public void removeSelectionChangedListener(
			ISelectionChangedListener listener) {
		if (listeners != null) 
			listeners.remove(listener);
	}

	@Override
	public void setSelection(ISelection solutionSimulator) {
		SelectionChangedEvent event = new SelectionChangedEvent(this, solutionSimulator);
		for(ISelectionChangedListener lis: listeners)
			lis.selectionChanged(event);
	}
}
